//系统菜单Layer

var onSysMenuSceneLoaded = [];

var sysMenuScene = {
	scene : null,
	layer : null,
	
	resource : [
		resource.SysMenu_png,
		resource.SysMenu_plist,
		resource.scale9white100100_png,
		resource.scale9black100100_png,
		resource.scale9halfwhite100100_png,
		resource.scale9halfblack100100_png,
		resource.Main_Theme_mp3,
		resource.click_wav
	],
	
	Create : function(){
		this.scene = new cc.Scene();
		this.layer = new SysMenu();
		this.scene.addChild(this.layer);
		
		for (var idx in onSysMenuSceneLoaded)
			onSysMenuSceneLoaded[idx]();
		
		return this.scene;
	}
};

var SysMenu = cc.Layer.extend({
	logo : null,
	
	clippedmenu : null,
	
	fadeinDelay : 3,
	
	ctor:function () {
		this._super();

		var winSize = cc.director.getWinSize();
		
		cc.spriteFrameCache.addSpriteFrames(resource.SysMenu_plist);
		
		//add background
		var background = new cc.Sprite(resource.background_png);
		background.attr({
			x: winSize.width / 2,
			y: winSize.height / 2,
			scale: SC});
		this.addChild(background, 0);
		
		this.logo = new cc.Sprite(resource.logoblank_png);
		this.logo.attr({
			x: winSize.width / 2,
			y: winSize.height * 0.78,
			scale: SC,
			opacity: 0
		});
		this.logo.setCascadeOpacityEnabled(true);
		this.addChild(this.logo, 2);
		
		//add fire
		var firecliper = new cc.DrawNode();
		var rect = [
		  cc.p(this.logo.width * 0.05, this.logo.height * 0.15),
		  cc.p(this.logo.width * 0.05, this.logo.height * 0.85),
		  cc.p(this.logo.width * 0.95, this.logo.height * 0.85),
		  cc.p(this.logo.width * 0.95, this.logo.height * 0.15)];
		var black = cc.color(0, 0, 0, 255);
		firecliper.drawPoly(rect, black, 4, black);
		
		var fireup = new cc.Sprite(resource.logofire_png);
		fireup.attr({
			x: this.logo.width / 2,
			y: this.logo.height / 2,
			opacity: 0
		});
		var firedown = new cc.Sprite(resource.logofire_png);
		firedown.attr({
			x: this.logo.width / 2,
			y: this.logo.height / 2 - fireup.height + 1,
			opacity: 0
		});
		
		var clippedfire = new cc.ClippingNode();
		clippedfire.setStencil(firecliper);
		clippedfire.addChild(fireup, 0);
		clippedfire.addChild(firedown, 0);
		this.logo.addChild(clippedfire, -1);
		
		//action
		var fadein = cc.fadeIn(this.fadeinDelay);
		this.logo.runAction(fadein);
		
		var moveup = cc.moveBy(2, 0, fireup.height - 1);
		var placedown = cc.moveBy(0, 0, -fireup.height + 1);
		var delay = cc.delayTime(this.fadeinDelay);
		var repeat = cc.sequence(moveup, placedown).repeatForever();
		var whole = cc.sequence(delay, cc.fadeIn(9));
		firedown.runAction(whole);
		firedown.runAction(repeat);
		fireup.runAction(whole.clone());
		fireup.runAction(repeat.clone());
		
		//add menu background
		var bottomy = this.logo.y - this.logo.height * SC / 2;
		var menucliper = new cc.DrawNode();
		var rect2 = [cc.p(0, 0), cc.p(0, bottomy), cc.p(winSize.width, bottomy), cc.p(winSize.width, 0)];
		menucliper.drawPoly(rect2, black, 4, black);
		
		this.clippedmenu = new cc.ClippingNode();
		this.clippedmenu.setStencil(menucliper);
		this.addChild(this.clippedmenu, 1);
		
		//add version label
		var versionLabel = cc.LabelTTF.create("Version " + versionUnits + "." + versionJudge + "." + versionUI, "simhei", 26);
		versionLabel.attr({anchorX:0, anchorY:0, x:18, y:12, color:cc.color(225,225,225) });
		this.addChild( versionLabel );
		
		//volume & music
		setting.RefreshVolume();
		cc.audioEngine.playMusic(resource.Main_Theme_mp3, true);
		cc.audioEngine.playEffect(resource.click_wav);
	}
});